package com.hundsun.yr.universal.p2p.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.utils.HsNumberUtils;
import com.hundsun.yr.universal.p2p.utils.CommTool;

import java.math.BigDecimal;


/**
 * Created by xx on 2017/8/18.
 */

public class CalculatorView extends LinearLayout implements View.OnClickListener,TextWatcher{
    private TextView tv_expected_revenue;
    private TextView tv_invest_amount;
    private TextView tv_num1;
    private RelativeLayout rl_num2;
    private TextView tv_num3;
    private TextView tv_num4;
    private RelativeLayout rl_num5;
    private TextView tv_num6;
    private TextView tv_num7;
    private RelativeLayout rl_num8;
    private TextView tv_num9;
    private FrameLayout fl_withdraw_calculator;
    private RelativeLayout rl_num0;
    private FrameLayout fl_backspace;
    private StringBuffer num = new StringBuffer();
    private OnDeleteButtonListener onDeleteButtonListener = null;

    /**
     * 设置默认计算方式
     */
    public BigDecimal rate = new BigDecimal(7.5);
    public BigDecimal term = new BigDecimal(360);
    public String pay_method = "1";


    public CalculatorView(Context context) {
        this(context,null);
    }

    public CalculatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CalculatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    /**
     * 初始化计算器
     * @param attrs
     */
    private void init(Context context,AttributeSet attrs){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.calculator, this, true);

        tv_expected_revenue = (TextView) findViewById(R.id.tv_expected_revenue);
        tv_invest_amount = (TextView) findViewById(R.id.tv_invest_amount);
        tv_num1 = (TextView) findViewById(R.id.tv_num1);
        rl_num2 = (RelativeLayout) findViewById(R.id.rl_num2);
        tv_num3 = (TextView) findViewById(R.id.tv_num3);
        tv_num4 = (TextView) findViewById(R.id.tv_num4);
        rl_num5 = (RelativeLayout) findViewById(R.id.rl_num5);
        tv_num6 = (TextView) findViewById(R.id.tv_num6);
        tv_num7 = (TextView) findViewById(R.id.tv_num7);
        rl_num8 = (RelativeLayout) findViewById(R.id.rl_num8);
        tv_num9 = (TextView) findViewById(R.id.tv_num9);
        fl_withdraw_calculator = (FrameLayout) findViewById(R.id.fl_withdraw_calculator);
        rl_num0 = (RelativeLayout) findViewById(R.id.rl_num0);
        fl_backspace = (FrameLayout) findViewById(R.id.fl_backspace);

        tv_num1.setOnClickListener(this);
        rl_num2.setOnClickListener(this);
        tv_num3.setOnClickListener(this);
        tv_num4.setOnClickListener(this);
        rl_num5.setOnClickListener(this);
        tv_num6.setOnClickListener(this);
        tv_num7.setOnClickListener(this);
        rl_num8.setOnClickListener(this);
        tv_num9.setOnClickListener(this);
        fl_withdraw_calculator.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //单独进行处理，对外暴露监听
                onDeleteButtonListener.onDeleteButtonClick();
            }
        });
        rl_num0.setOnClickListener(this);
        fl_backspace.setOnClickListener(this);

        tv_invest_amount.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() != R.id.fl_backspace && num.length() > 6){
            return;
        }

        switch (v.getId()){
            case R.id.tv_num1:
                num.append("1");
                break;
            case R.id.rl_num2:
                num.append("2");
                break;
            case R.id.tv_num3:
                num.append("3");
                break;
            case R.id.tv_num4:
                num.append("4");
                break;
            case R.id.rl_num5:
                num.append("5");
                break;
            case R.id.tv_num6:
                num.append("6");
                break;
            case R.id.tv_num7:
                num.append("7");
                break;
            case R.id.rl_num8:
                num.append("8");
                break;
            case R.id.tv_num9:
                num.append("9");
                break;
            case R.id.rl_num0:
                if (num.length() == 0){
                    return;
                }
                num.append("0");
                break;
            case R.id.fl_backspace:
                if (num.length() == 0){

                } else if (num.length() == 1){
                    num.delete(0,num.length());
                }/* else if (num.length() == 2 && num.indexOf(",") != -1){
                    num.delete(0,num.length());
                }*/else {
                    num.deleteCharAt(num.length() - 1);
                }
                break;
        }

        String money = HsNumberUtils.moneyThousandsFormat(new String(num));

        if (v.getId() == R.id.fl_backspace && num.length() == 0){
            tv_invest_amount.setText(money);
        } else {
            tv_invest_amount.setText("￥" + money);
        }
    }

    /**
     * 监听tv_invest_amount变化
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String s1 = s.toString().replace("￥", "").replace(",","");
        tv_expected_revenue.setText(CommTool.keep2Digits(setCalculateMeathod(new BigDecimal(s1))));
    }

    /**
     * 做一个方法用于设定计算方式
     */


    public void setInit(){
        num.delete(0,num.length());
        tv_invest_amount.setText("0");
        tv_expected_revenue.setText("0.00");
    }

    public void setDeleteButtonClick(OnDeleteButtonListener onDeleteButtonListener){
        this.onDeleteButtonListener = onDeleteButtonListener;
    }

    public interface OnDeleteButtonListener{
        void onDeleteButtonClick(); //单击事件处理接口
    }

    /**
     * 添加计算方式
     */
    public BigDecimal setCalculateMeathod(BigDecimal amount){
        try {
            BigDecimal monthRate = rate.divide(new BigDecimal(12), 10, BigDecimal.ROUND_HALF_EVEN);
            BigDecimal income = null;

            switch (pay_method) {
                case "1":
                    //到期还本息付
                    income = amount.multiply(rate).multiply(term).divide(new BigDecimal(360), 5, BigDecimal.ROUND_HALF_EVEN);
                    break;
                case "2":
                    //按月还息到期还本
                    income = amount.multiply(monthRate).multiply(term);
                    break;
                case "3":
                    //等额本息
                    BigDecimal pow = (monthRate.add(new BigDecimal(1))).pow(term.intValue());
                    income = (amount.multiply(monthRate).multiply(pow).divide(pow.subtract(new BigDecimal(1)), 10, BigDecimal.ROUND_HALF_EVEN)).multiply(term).subtract(amount);
                    break;
                case "4":
                    //等额本金
                    income = amount.multiply(monthRate).multiply((term.add(new BigDecimal(1)))).divide(new BigDecimal(2), 10, BigDecimal.ROUND_HALF_EVEN);
                    break;
                default:
                    income = amount.multiply(rate).multiply(term).divide(new BigDecimal(360), 10, BigDecimal.ROUND_HALF_EVEN);
                    break;
            }
            return income;
        } catch (Exception e) {
            return new BigDecimal(0);
        }
    }
}
